package com.siruier.boss.ui.activity.mall

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.help.Inputtips
import com.amap.api.services.help.InputtipsQuery
import com.amap.api.services.poisearch.PoiSearch
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.bean.LocationSelect
import com.siruier.boss.databinding.ActivityLocationSearchBinding
import com.siruier.boss.databinding.ItemLocBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.bean.picker.BaseCityBean
import com.siruier.boss.ui.ext.showLoadingBeforeSuccessCallback
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.ext.toColor
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.utils.Logger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext


class LocationSearchActivity : BaseActivity<ActivityLocationSearchBinding>() {

    private val cityCode: String by InjectBundle()
    private val mLocSearchAdapter = LocSearchAdapter()
    private lateinit var mLoadService: LoadService<*>

    override fun initView() {
        mLoadService = LoadSirHelper.register(vb.rvAddress, listener = ::search)
        mLoadService.showSuccess()
        vb.searchView.clearListener = ::search

        vb.rvAddress.layoutManager = LinearLayoutManager(applicationContext)
        vb.rvAddress.adapter = mLocSearchAdapter

        mLocSearchAdapter.setOnClickAdapterListenter { view, i ->
            setResult(RESULT_OK, Intent().apply {
                putExtra("locationSelect", mLocSearchAdapter.getItem(i))
            })
            finish()
        }
    }

    private fun search() {
        mLoadService.showLoadingCallback()
        launchUI({
            val poiSearchQuery = PoiSearch.Query(vb.searchView.text, null, cityCode)
            poiSearchQuery.pageSize = 20
            poiSearchQuery.pageNum = 1
            val poiSearch = PoiSearch(application, poiSearchQuery)
            val poiResult = withContext(Dispatchers.IO) { poiSearch.searchPOI() }
            val datas = poiResult.pois.mapTo(mutableListOf()) { pt ->
                LocationSelect(
                    BaseCityBean(pt.adCode, pt.provinceName),
                    BaseCityBean(pt.adCode, pt.cityName),
                    BaseCityBean(pt.adCode, pt.adName),
                    pt.title, pt.snippet, pt.provinceName + pt.cityName + pt.adName + pt.snippet
                )
            }
            mLocSearchAdapter.datas = datas
            mLocSearchAdapter.notifyDataSetChanged()
            Logger.e(JSON.toJSONString(poiResult))
        }, ApiObserver(loadService = mLoadService))
    }

    class LocSearchAdapter : BaseRecyclerAdapter<ItemLocBinding, LocationSelect>() {

        override fun onBindViewHolder(vb: ItemLocBinding, bean: LocationSelect, position: Int) {
            vb.ivLoc.setImageResource(R.mipmap.icon_un_loc)
            vb.tvTitle.setTextColor(R.color.text_color.toColor())
            vb.tvTitle.text = bean.title
            vb.tvAddress.text = bean.address
            setViewClick(position, vb.root)
        }
    }

}